#define _CRT_SECURE_NO_WARNINGS 1
#include "string.h"

using namespace yjt;
string::string(const char* str = '\0')
{
	_capacity = strlen(str);
	_size = _capacity;
	_str = new char[_capacity + 1];
	strcpy(_str, str);
}
//s1(s2)
string::string(const string& s)
{
	_capacity = s._capacity;
	_size = s._size;
	_str = new char[_capacity + 1];
	strcpy(_str, s._str);
}
string& string::operator=(const string& s)
{
	if (this != &s)
	{
		_capacity = s._capacity;
		_size = s._size;
		_str = new char[_capacity + 1];
		strcpy(_str, s._str);
	}
}
string::~string()
{
	delete[] _str;
	_str = nullptr;
	_size = 0;
	_capacity = 0;
}
void string::reserve(size_t n)
{
	if (n > _capacity)
	{
		char* tmp = new char[n + 1];
		strcpy(tmp, _str);
		delete[] _str;
		_str = tmp;
		_capacity = n;
	}
}
void string::push_back(char ch)
{
	if (_size == _capacity)
	{
		size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
		reserve(newcapacity);
	}
	_str[_size] = ch;
	_size++;
	_str[_size] = '\0';
}
void string::erase(int pos, int len)
{
	if (len == npos || pos + len > _size)
	{
		_str[0] = '\0';
		_size = pos;
	}
	else
	{
		strcpy(_str + pos, _str + pos + len);
		_size -= len;
	}
}